package com.htandroid.htandroid.webservice;

import com.htandroid.htandroid.entity.CgglCgList;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class abXMLCgglscshlist {

    /**
     * 解析评论xml信息
     *
     * @param content
     * @return
     */
    public static ArrayList<CgglCgList> paraseCommentInfors(String content) {
        List<String> list = new ArrayList<String>();
        content = content.replace("CgglCgList=anyType", "#=#");
        String[] strs = content.split("#=");
        for (int i = 0; i < strs.length; i++) {
            if (strs[i].contains("#")) {
                list.add(strs[i].replace("#", "").trim().replace(" ", ""));
            }
        }
        ArrayList<CgglCgList> cgglscsh = new ArrayList<CgglCgList>();
        for (int i = 0; i < list.size(); i++) {
            if (i == list.size() - 1) {
                list.set(i, list.get(i).replace(";};};}", "\"}"));
            }
            list.set(i, list.get(i).replace(";};", "\"}").replace("=", "\":\"").replace(";", "\",\"").replace("{binvexchtype", "{\"binvexchtype"));
            try {
                CgglCgList info = new CgglCgList();
                JSONObject dataJson = new JSONObject(list.get(i));
                String dDate_DH = dataJson.getString("dDate_DH").substring(0,10)+" "+dataJson.getString("dDate_DH").substring(10,dataJson.getString("dDate_DH").length());
                String ddate = dataJson.getString("ddate").substring(0,10)+" "+dataJson.getString("ddate").substring(10,dataJson.getString("ddate").length());
                info.setBinvexchtype(string2null(dataJson.getString("binvexchtype")));
                info.setBinvtype(string2null(dataJson.getString("binvtype")));
                info.setcQGType(string2null(dataJson.getString("cQGType")));
                info.setCchangedate(string2null(dataJson.getString("cchangedate")));
                info.setCdefine1(string2null(dataJson.getString("cdefine1")));
                info.setCdefine2(string2null(dataJson.getString("cdefine2")));
                info.setCdefine3(string2null(dataJson.getString("cdefine3")));
                info.setCdefine4(string2null(dataJson.getString("cdefine4")));
                info.setCdepcode(string2null(dataJson.getString("cdepcode")));
                info.setCdepname(string2null(dataJson.getString("cdepname")));
                info.setCinva_unit(string2null(dataJson.getString("cinva_unit")));
                info.setCinvcode(string2null(dataJson.getString("cinvcode")));
                info.setCinvm_unit(string2null(dataJson.getString("cinvm_unit")));
                info.setCinvname(string2null(dataJson.getString("cinvname")));
                info.setCinvstd(string2null(dataJson.getString("cinvstd")));
                info.setCmaker(string2null(dataJson.getString("cmaker")));
                info.setCprechecker(string2null(dataJson.getString("cprechecker")));
                info.setCreplaceitem(string2null(dataJson.getString("creplaceitem")));
                info.setCstatus(string2null(dataJson.getString("cstatus")));
                info.setCvencode(string2null(dataJson.getString("cvencode")));
                info.setCvenname(string2null(dataJson.getString("cvenname")));
                info.setCvouchid(string2null(dataJson.getString("cvouchid")));
                info.setCvouchid_qgd(string2null(dataJson.getString("cvouchid_qgd")));
                info.setCvouchnum(string2null(dataJson.getString("cvouchnum")));
                info.setCvouchnum_qgd(string2null(dataJson.getString("cvouchnum_qgd")));
                info.setdDate_DH(string2null(dDate_DH));
                info.setDdate(string2null(ddate));
                info.setIamount(string2null(dataJson.getString("iamount")));
                info.setId(string2null(dataJson.getString("id")));
                info.setIinvexchrate(string2null(dataJson.getString("iinvexchrate")));
                info.setInum(string2null(dataJson.getString("inum")));
                info.setInum_sh(string2null(dataJson.getString("inum_sh")));
                info.setIprice(string2null(dataJson.getString("iprice")));
                info.setIquantity(string2null(dataJson.getString("iquantity")));
                info.setIquantity_jy(string2null(dataJson.getString("iquantity_jy")));
                info.setIquantity_sh(string2null(dataJson.getString("iquantity_jy")));
                info.setS_cdefine1(string2null(dataJson.getString("s_cdefine1")));
                info.setS_cdefine2(string2null(dataJson.getString("s_cdefine2")));
                info.setS_cdefine3(string2null(dataJson.getString("s_cdefine3")));
                info.setS_cdefine4(string2null(dataJson.getString("s_cdefine4")));
                info.setVenplatid(string2null(dataJson.getString("venplatid")));
                info.setYdpriceup(string2null(dataJson.getString("ydpriceup")));
                info.setYdvenup(string2null(dataJson.getString("ydvenup")));
                cgglscsh.add(info);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return cgglscsh;
    }

    //函数作用是把null 转为空
    public static String string2null(String str) {
        String rtn = "";
        if (str == null || str.equals("null") || str.equals("anyType{}")) {
            rtn = "";
        } else {
            rtn = str;
        }
        return rtn;
    }
}
